package cn.tedu.eguser1.controller;

import cn.tedu.eguser1.base.response.JsonResult;

import cn.tedu.eguser1.base.response.StatusCode;
import cn.tedu.eguser1.mapper.UserMapper;
import cn.tedu.eguser1.pojo.dto.UserAddParam;
import cn.tedu.eguser1.pojo.dto.UserUpdateParam;
import cn.tedu.eguser1.pojo.entity.User;
import cn.tedu.eguser1.pojo.vo.UserListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;
import org.apache.ibatis.annotations.Options;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@Tag(name = "01-用户管理模块")
@Slf4j
@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    /**
     * 添加用户
     *
     * @param userAddParam
     * @return
     */
    @PostMapping("add")
    @Operation(summary = "添加用户")
    @ApiOperationSupport(order = 10)
    @Parameters(value = {
            @Parameter(name = "username", description = "用户名", required = true, example = "1"),
            @Parameter(name = "password", description = "密码", required = true, example = "1"),
            @Parameter(name = "nickname", description = "昵称", required = true, example = "1"),
            @Parameter(name = "userAddParam",hidden = true)
    })
    public JsonResult add(UserAddParam userAddParam) {
        System.out.println("userAddParam = " + userAddParam);
        User user = new User();
        BeanUtils.copyProperties(userAddParam,user);
        user.setId(1L);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userMapper.insertUser(user);
        return JsonResult.ok();
    }
    /**
     * 查看用户
     */
    @GetMapping("list")
    @Operation(summary = "查看用户")
    @ApiOperationSupport(order = 20)

    public  JsonResult list() {
        List<UserListVO> userListVO = userMapper.selectUserList();

        return JsonResult.ok(userListVO);
    }

    /**
     * 删除用户
     */
    @DeleteMapping("del")
    @Operation(summary = "删除用户")
    @ApiOperationSupport(order = 30)
    @Parameter(name = "id", description = "用户编号", required = true, example = "1")

    public JsonResult del(Long id) {
        System.out.println("id = " + id);
        int count = userMapper.deleteUser(id);
        if (count > 0) {
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERYION_FAIL);
    }

    @PostMapping("update")
    @Operation(summary = "更新用户")
    @ApiOperationSupport(order = 40)
    @Parameters(value = {
            @Parameter(name = "id", description = "用户编号", required = true, example = "1"),
            @Parameter(name = "username", description = "用户名", required = true, example = "1"),
            @Parameter(name = "password", description = "密码", required = true, example = "1"),
            @Parameter(name = "nickname", description = "昵称", required = true, example = "1"),
            @Parameter(name ="userUpdateParam",hidden = true)
    })
    public JsonResult update(UserUpdateParam userUpdateParam) {
        System.out.println("userUpdateParam = " + userUpdateParam);
        User user = new User();
        BeanUtils.copyProperties(userUpdateParam, user);
        user.setUpdateTime(new Date());
        int count = userMapper.updateUser(user);
        if (count > 0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERYION_FAIL);
    }
}
